package com.spring.annotation.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Title：@Conditional 条件判断注解（用条件限制组件的创建）
 * 条件类：符合条件的组件才会创建
 */
public class DemoCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取ioc容器使用的beanFactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        //获取类加载器
        ClassLoader classLoader = conditionContext.getClassLoader();
        //获取当前环境（java运行环境所在的环境）
        Environment environment = conditionContext.getEnvironment();//
        //获取定义bean的注册类
        BeanDefinitionRegistry registry = conditionContext.getRegistry();
        //获取资源加载器
        ResourceLoader resourceLoader = conditionContext.getResourceLoader();

        String property = environment.getProperty("os.name");
        if (property.contains("Windows 10")){
            System.out.println("输出信息：==>"+property);
            return true;
        }

        //默认返回false，前面通过获取到的信息进行if条件判断，符合则return true
        return false;
    }
}